package com.nidu.demo.file.executor.query;

import com.alibaba.cola.dto.PageResponse;
import com.nidu.demo.common.enums.FileStorageTypeEnum;
import com.nidu.demo.file.convertor.FileConfigConvertor;
import com.nidu.demo.file.convertor.FileConfigCriteriaConvertor;
import com.nidu.demo.file.dto.FileConfigCO;
import com.nidu.demo.file.dto.FileConfigPageQry;
import com.nidu.demo.file.gateway.FileConfigGateway;
import com.nidu.demo.file.model.FileConfig;
import com.nidu.demo.file.model.FileConfigCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 文件配置分页查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class FileConfigPageQryExe {

    private final FileConfigGateway gateway;
    private final FileConfigConvertor convertor;
    private final FileConfigCriteriaConvertor criteriaConvertor;

    /**
     * 分页查询文件配置
     *
     * @param qry 分页查询条件
     * @return 分页结果
     */
    public PageResponse<FileConfigCO> execute(FileConfigPageQry qry) {
        // 1. 转换查询条件
        FileConfigCriteria criteria = criteriaConvertor.toDataObject(qry);
        
        // 2. 执行分页查询
        PageResponse<FileConfig> pageResponse = gateway.pageByCondition(criteria);
        
        // 3. 转换为DTO
        List<FileConfigCO> fileConfigCOS = convertor.toClientObjects(pageResponse.getData());
        
        // 4. 设置存储类型名称
        for (FileConfigCO fileConfigCO : fileConfigCOS) {
            try {
                FileStorageTypeEnum storageType = FileStorageTypeEnum.valueOf(fileConfigCO.getStorage());
                fileConfigCO.setStorageName(storageType.getName());
            } catch (Exception e) {
                fileConfigCO.setStorageName("未知类型");
            }
        }
        
        return PageResponse.of(
                fileConfigCOS,
                pageResponse.getTotalCount(),
                pageResponse.getPageSize(),
                pageResponse.getPageIndex()
        );
    }
}
